// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Text;

namespace SnapObjects.Data.Odbc
{
    internal class OdbcHanaSqlFunctionStatementGenerator : SqlFunctionStatementGenerator
    {
        public OdbcHanaSqlFunctionStatementGenerator(
            ISqlStatementFactory factory,
            ISqlGenerationHelper helper,
            ITypeMapper typeMapper) : base(factory, helper, typeMapper)
        {
        }

        protected override void CreateFrom(
            StringBuilder sqlBuilder, SqlStatement statement, string functionName)
        {
            var function = (SqlFunctionStatement)statement;

            if (function.ReturnType == FunctionReturnType.Scalar)
            {
                sqlBuilder.Append(SqlWhitespaceToken.Whitespace);
                sqlBuilder.Append(new SqlKeywordToken(SqlKeywords.FROM));
                sqlBuilder.Append(SqlWhitespaceToken.Whitespace);
                sqlBuilder.Append("DUMMY");
            }
            else
            {
                sqlBuilder.Append(SqlWhitespaceToken.Whitespace);
                sqlBuilder.Append(new SqlKeywordToken(SqlKeywords.FROM));
                sqlBuilder.Append(SqlWhitespaceToken.Whitespace);
                sqlBuilder.Append(functionName);

                this.CreateParameters(sqlBuilder, statement);
            }
        }
    }
}
